package com.vcc.msc.sbs.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.vcc.msc.sbs.R;
import com.vcc.msc.sbs.entity.WesEntity;
import com.vcc.msc.sbs.proxy.ServiceLocator;
import com.vcc.msc.sbs.proxy.WesProxy;

public class ShowResultExpandableListViewAdapter extends
		BaseExpandableListAdapter {

	private List<WesEntity> groups;
	private Context context;

	public ShowResultExpandableListViewAdapter(Context context,
			List<WesEntity> groups) {
		Log.d(this.getClass().getName(), "ShowResultExpandableListViewAdapter");
		this.context = context;
		this.groups = groups;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		WesEntity wesEntity = groups.get(groupPosition);
		// TODO Call WS inside thread
		WesProxy wesProxy = ServiceLocator.getWesProxy();

		WesEntity wesDetails = wesProxy.getWesDetails(wesEntity.getVersion(),
				wesEntity.getTypeCode(), wesEntity.getWesId());

		// TODO temp fix.
		byte[] img = wesProxy.getWesImage();
		if (img != null) {
			List<byte[]> imageArray = new ArrayList<byte[]>();
			imageArray.add(img);
			wesDetails.setImageArray(imageArray);
		} else {
			// NO Image Mock
			// List<byte[]> imageArray = new ArrayList<byte[]>();
			// Bitmap mockImg1 = BitmapFactory.decodeResource(
			// context.getResources(), R.drawable.bild1);
			// ByteArrayOutputStream bos = new ByteArrayOutputStream();
			// mockImg1.compress(CompressFormat.PNG, 0 /* ignored for PNG */,
			// bos);
			// byte[] bitmapdata1 = bos.toByteArray();

			// Bitmap mockImg2 = BitmapFactory.decodeResource(
			// context.getResources(), R.drawable.toolbox);
			// ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
			// mockImg2.compress(CompressFormat.PNG, 0 /* ignored for PNG */,
			// bos1);
			// byte[] bitmapdata2 = bos1.toByteArray();

			// imageArray.add(bitmapdata1);
			// // imageArray.add(bitmapdata2);
			// wesDetails.setImageArray(imageArray);
		}
		return wesDetails;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		final WesEntity wesDetails = (WesEntity) getChild(groupPosition,
				childPosition);

		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.child_layout, null);
		}

		// New
		Gallery gallery = (Gallery) convertView.findViewById(R.id.wesGallery);
		gallery.setAdapter(new ImageAdapter(parent.getContext(), wesDetails
				.getImageArray()));

		gallery.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(final AdapterView parent, final View view,
					final int position, long id) {
				Dialog d = new Dialog(context,
						android.R.style.Theme_InputMethod);
				d.setContentView(R.layout.full_image);
				ImageView imageSrc = (ImageView) view;
				ImageView imageDes = (ImageView) d.findViewById(R.id.fullImage);
				imageDes.setBackgroundDrawable(imageSrc.getDrawable());
				d.show();
				WindowManager.LayoutParams lp = d.getWindow().getAttributes();
				lp.dimAmount = 0.9f;
				d.getWindow().setAttributes(lp);
				d.getWindow().addFlags(
						WindowManager.LayoutParams.FLAG_DIM_BEHIND);

			}
		});

		gallery.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(final AdapterView parent,
					final View view, final int position, long id) {
				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setCancelable(false)
						.setTitle("Do you want to delete?")
						.setPositiveButton("Delete",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										Toast.makeText(context,
												"To be deleted in real :-)",
												Toast.LENGTH_SHORT).show();
										// TODO Remove from gallery
										// wesDetails.getImageArray().remove(
										// position);
										// Gallery gallery = (Gallery) parent
										// .findViewById(R.id.wesGallery);
										// gallery.setAdapter(new ImageAdapter(
										// context, wesDetails
										// .getImageArray()));
									}
								})
						.setNegativeButton("Cancel",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {

									}
								}).show();

				return false;
			}
		});

		TextView wesId = (TextView) convertView.findViewById(R.id.resultWesId);
		TextView wesName = (TextView) convertView
				.findViewById(R.id.resultWesName);
		ImageView image = (ImageView) convertView
				.findViewById(R.id.photoResultView);
		image.setImageBitmap(null);
		wesId.setText(wesDetails.getWesId());
		wesName.setText(wesDetails.getWesName());

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;// TODO change latter
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		WesEntity group = (WesEntity) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.group_layout, null);
		}

		TextView wesName = (TextView) convertView.findViewById(R.id.wesName);
		wesName.setText(group.getWesName());

		TextView wesId = (TextView) convertView.findViewById(R.id.wesId);
		wesId.setText(group.getWesId());

		TextView cdsId = (TextView) convertView.findViewById(R.id.cdsId);
		cdsId.setText(group.getCdsId());

		TextView date = (TextView) convertView.findViewById(R.id.date);
		date.setText(group.getDate());

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
